package com.buba.manager.service.impl;

import com.buba.common.util.JsonUtil;
import com.buba.manager.mapper.BookMapper;
import com.buba.manager.pojo.Book;
import com.buba.manager.service.BookService;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.HtmlUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 图书业务层
 */
@Service
public class BookServiceImpl implements BookService {
    private BookMapper bookMapper;

    //引入redisTemplate
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //获取redis的key
    @Value("{goods.category.list.key}")
    private String goodsCategoryListKey;


    @Override
    public Integer addBook(Book book) {
        //数据改变后 清除本地缓存
        redisTemplate.delete(redisTemplate.keys("goods*"));
        //判断是否存在富文本编辑内容
        if(null!=book.getBookIntroduce()){
            return bookMapper.addBook( book);
        }else {
            //如果存在数据 完成转义后存入
            book.setBookIntroduce(HtmlUtils.htmlEscape(book.getBookIntroduce()));
            return bookMapper.addBook( book);
        }

    }

    /**
     * 无条件查询处理redis
     * @param map
     * @return
     */
    @Override
    public List<Book> selBook(Map<String, Object> map) {


        //创建缓存对象
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        //获取redis中的key的值
        String gcvList = valueOperations.get(goodsCategoryListKey);
        //判断redis中是否有数据 有数据直接转换格式返回 没有数据再数据库中查询
        if(!StringUtils.isNullOrEmpty(gcvList)){
            return JsonUtil.string2Obj(gcvList, List.class, Book.class);
        }else {
            List<Book> books = bookMapper.selBook(map);
            //在缓存中加入数据
            valueOperations.set(goodsCategoryListKey, JsonUtil.obj2String(books));
            return books;
        }



    }

    /**
     * 携带条件查询的数据怎么处理redis
     * @param map
     * @return
     */
    @Override
    public List<Book> selBookByCondition(Map<String, Object> map) {
        //假设map中有查询条件
        String pageNum = String.valueOf(map.get("pageNum"));
        String pageSize = String.valueOf(map.get("pageSize"));
        String name = String.valueOf(map.get("name"));
        String age = String.valueOf(map.get("age"));
        String sex = String.valueOf(map.get("sex"));
        //首先定义一个rediskey数组
        String[] booksArrays = {
            "books:pageNum" + pageNum +":pageSize"+pageSize+":",
                "name:",
                "age:",
                "sex:"
        };
        //判断条件1是否为空
        if(!StringUtils.isNullOrEmpty(name)){
            booksArrays[1] = "name" +name +":";
        }
        //判断条件2是否为空
        if(!StringUtils.isNullOrEmpty(age)){
            booksArrays[2] = "age" +age +":";
        }
        //判断条件3是否为空
        if(!StringUtils.isNullOrEmpty(sex)){
            booksArrays[3] = "sex" +sex +":";
        }
        //拼接完整的rediskey
        String keyList = Arrays.stream(booksArrays).collect(Collectors.joining());


        //创建缓存对象
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        //获取redis中的key的值
        String bookList = valueOperations.get(keyList);
        //如果redis中的数据不为空 直接返回
        if(!StringUtils.isNullOrEmpty(bookList)){
            return JsonUtil.string2Obj(bookList, List.class, Book.class);
        }else {
            List<Book> bookslist = bookMapper.selBookByCondition(map);
            //检查是否有对应的数据
            if (!CollectionUtils.isEmpty(bookslist)){
                //在缓存中加入数据
                valueOperations.set(keyList, JsonUtil.obj2String(bookslist));
            }else {
                //如果数据为空在缓存中添加有失效时长的key
                valueOperations.set(keyList, JsonUtil.obj2String(bookslist),60, TimeUnit.SECONDS);
            }
            return bookslist;
        }



    }

    @Override
    public Integer delBook(List<String> list) {
        //数据改变后 清除本地缓存
        redisTemplate.delete(redisTemplate.keys("goods*"));
        return bookMapper.delBook( list);
    }

    @Override
    public long selCount() {
        return bookMapper.selCount( );
    }

    @Override
    public Boolean updBook(Book book) {
        return bookMapper.updBook(book );
    }



    @Autowired
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }
}
